
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'warehouse/outinventory/index',
                    add_url: 'warehouse/outinventory/add',
                    edit_url: 'warehouse/outinventory/edit',
                    del_url: 'warehouse/outinventory/delete',
                    multi_url: 'warehouse/outinventory/multi',
                    import_url: 'warehouse/outinventory/import',
                    table: 'out_inventory',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'create_time',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'outorderid', title: __('Outorderid')},
                        {field: 'orderid', title: __('OrderNo')},
                        {field: 'trackorderid', title: __('Trackorderid'),operate:false},
                        {field: 'logistics', title: __('Logistics'),operate:false},
                        {field: 'provider', title: __('Provider'),operate:false},
                        {field: 'create_time', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'items', title: __('ItemInfo'),operate:false,formatter:Controller.api.formatter.itemFormat,events:Controller.api.events.getItems},
                        {field: 'status', title: __('Status'),formatter:Controller.api.formatter.checkstatus},
                        {field: 'itemsku', title: __('产品SKU'),visible:false},
                        {field: 'logistic', title: '物流商类型',operate:false},
                        {field: 'remark', title: __('Remark'),operate:false},
                        {
                          field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                          buttons: [
                               {
                                name: 'outdetail',
                                title: '出库单据详情',
                                classname: 'btn btn-xs btn-info  btn-dialog',
                                icon: 'fa fa-eye',
                                url: 'warehouse/outinventory/detail',
                                callback: function (data) {
                                    Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                },
                            },
                          ],
                          formatter: Table.api.formatter.operate
                      }
                    ]
                ],
                search:false,
                showToggle: false,
                searchFormVisible:true,
                exportDataType: 'selected',
                exportTypes: ['doc', 'excel']
            });

            

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                checkstatus: function (value, row, index) {
                if(value == 1000){
                  return '<span class="status_audit">已审核</span>';
                }else if(value == 2000){
                    return '<span class="status_audit">部分出库</span>';
                }else if(value == 2001){
                    return '<span class="status_reject">已拒绝</span>';
                }else if(value == 3000){
                    return '<span class="status_checked">已确认</span>';
                }else{
                    return '<span class="status_checking">审核中</span>';
                }

                },
                itemFormat: function (value,row,index) {

                  var itemArray = value.split('\\r\\n');
                  var itemString = "<span class='el-tag' title='点击查看所有商品信息'>";
                  // itemArray.forEach(element => {
                  //   itemString = itemString+"<a>"+element+"</a>";
                  // });
                  for(let i=0;i<itemArray.length;i++){
                    if(i>=1 && i<= itemArray.length-1){
                      itemString = itemString+"</br>"
                    }
                    itemString = itemString+"<a>"+itemArray[i]+"</a>";
                  }
                  return itemString + "</span>";
                  // return "<span class='el-tag' title='点击查看所有商品信息'><a>3333</a></br><a>6666</a></span>";
                  //return "<span class='el-tag' title='点击查看所有商品信息'>"+value+"</span>";
                }
            },
            events:{
                getItems:{
                    'click .el-tag': function (e, value, row, index) {
                        e.stopPropagation();
                        Fast.api.ajax({
                                url: 'warehouse/outinventory/items',
                                data: {outorderid: row.outorderid}
                            }, function (data, ret) {
                                var items = "";
                                for(i=0;i<data.length;i++){
                                    items += "<span>"+data[i]['itemname']+" - " + data[i]['itemsku']+ " * "+ data[i]['goods_count']+"</span><br/>";
                                }
                                Layer.alert(items);
                                return false;
                            },function (data,ret) {
                                alert(ret.msg);
                            }
                        );

                    }
                }
            }
        }
    };
    return Controller;
});